package com.sff.easyexcel.test.demo.template;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.util.ListUtils;
import com.google.common.collect.Sets;
import com.sff.easyexcel.test.demo.merge.CellMergeStrategy;
import com.sff.easyexcel.test.demo.util.DateUtil;
import com.sff.easyexcel.test.demo.util.FilePathUtil;

import java.util.List;
import java.util.Random;

/**
 * DEMO-2:动态表头和导出单元格数据
 */
public class AttExportDataTemplateSecond {

    public static void main(String[] args) {

        String writeFileName = FilePathUtil.getPath() + "att-second-" + System.currentTimeMillis() + ".xlsx";
        EasyExcel.write(writeFileName)
                .head(createDynamicHead())
                .registerWriteHandler(new CellMergeStrategy(0, 0, Sets.newHashSet(0, 1, 2)))
                .excelType(ExcelTypeEnum.XLSX)
                .sheet("考勤报表")
                .doWrite(createDataByList());
    }

    private static List<List<Object>> createDataByList() {
        List<List<Object>> list = ListUtils.newArrayList();

        for (int i = 0; i < 2; i++) {

            List<Object> data = ListUtils.newArrayList();
            data.add("RY044" + i);
            data.add("张三" + i);
            data.add("技术部");

            data.add("出勤工时");
            List<Integer> dayKeyList = DateUtil.getBetweenDateKey(1643644800L, 1646063999L);
            for (Integer integer : dayKeyList) {
                data.add("");
            }
            list.add(data);

            List<Object> data1 = ListUtils.newArrayList();
            data1.add("RY044" + i);
            data1.add("张三" + i);
            data1.add("技术部");
            data1.add("加班工时");
            for (Integer integer : dayKeyList) {
                int hour = new Random().nextInt(5);
                if (0 == hour) {
                    data1.add("");
                } else {
                    data1.add(new Random().nextInt(5));
                }
            }
            list.add(data1);

            List<Object> data2 = ListUtils.newArrayList();
            data2.add("RY044" + i);
            data2.add("张三" + i);
            data2.add("技术部");

            data2.add("考勤补贴");
            for (Integer integer : dayKeyList) {
                data2.add("");
            }
            list.add(data2);

            List<Object> data3 = ListUtils.newArrayList();
            data3.add("RY044" + i);
            data3.add("张三" + i);
            data3.add("技术部");

            data3.add("请假旷工");
            for (Integer integer : dayKeyList) {
                data3.add("");
            }
            list.add(data3);
        }

        return list;
    }

    /**
     * 创建动态日期表头
     */
    private static List<List<String>> createDynamicHead() {
        List<List<String>> list = ListUtils.newArrayList();

        List<String> jobNo = ListUtils.newArrayList();
        jobNo.add("202201月考勤报表");
        jobNo.add("工号");
        list.add(jobNo);

        List<String> name = ListUtils.newArrayList();
        name.add("202201月考勤报表");
        name.add("姓名");
        list.add(name);

        List<String> dept = ListUtils.newArrayList();
        dept.add("202201月考勤报表");
        dept.add("部门");
        list.add(dept);

        List<String> attendanceItem = ListUtils.newArrayList();
        attendanceItem.add("202201月考勤报表");
        attendanceItem.add("考勤项");
        list.add(attendanceItem);

        /**2022-02-01 到 2022-02-28*/
        List<Integer> dayKeyList = DateUtil.getBetweenDateKey(1643644800L, 1646063999L);
        for (Integer day : dayKeyList) {
            List<String> oneDay = ListUtils.newArrayList();
            oneDay.add("202201月考勤报表");
            oneDay.add(day + "");
            list.add(oneDay);
        }

        List<String> total = ListUtils.newArrayList();
        total.add("202201月考勤报表");
        total.add("合计");
        list.add(total);
        return list;
    }


}
